package com.contractpage.ec;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.plugins.Page;
import com.compattern.common.alternation.InvokeResultWithGenericType;
import com.compattern.po.ProjectBespeakDetailPO;
import com.compattern.service.BespeakInfoService;
import com.compattern.service.MarketingProjectService;
import com.contractpage.ec.entity.Appoint;
import com.contractpage.ec.entity.ContractDetail;
import com.contractpage.ec.service.AppointService;
import com.richgo.thrsys.entity.contract.TgetAllReserveMsgByStatusVO;
import com.richgo.thrsys.service.contract.TPaperContractService;

@Component
public class MyScheduler {
	@Autowired
	private TPaperContractService tpContractService;
	
	@Autowired
	private AppointService appointService;
	
	/*@Autowired
	private MarketingProjectService marktingProjectService;*/
	
	//@Autowired
	private BespeakInfoService  bespeakInfoService;
	
	private Appoint buildAppoint(TgetAllReserveMsgByStatusVO reser){
		Appoint app = new Appoint();
		//app.setId(String.valueOf(System.nanoTime()));
		//if(per) app.preInsert("");
		app.setReserveNo(reser.getReserveNo());
		app.setDepartmentcode(reser.getDepartNo());
		app.setProjectcode(reser.getProserialNo()==null?"":reser.getProserialNo().toString());
		app.setStatus("0");
		app.setCustcode(reser.getCustNo());
		app.setCustname(reser.getCustName());
		app.setCredentialnumber(reser.getIdentityNo());
		app.setAccount("");
		app.setBank(reser.getBankName());
		app.setBankno(reser.getBankAcco());
		app.setTransfercash(reser.getTotalPayBalance());
		app.setTransferdate(reser.getCapitalDate());
		app.setIscode(reser.getBrokerAccount());
		app.setReservationSuccessDate(reser.getReservationSuccessDate());
		app.setReserveStatus(reser.getReserveStatus());
		app.setReserveDate(reser.getReserveDate());
		return app;
	}
	
	private Appoint buildAppoint(ProjectBespeakDetailPO pro){
		Appoint app = new Appoint();
		//if(per) app.preInsert("");
		//app.setId(String.valueOf(System.nanoTime()));
		app.setReserveNo(pro.getBespeakCode());
		//app.setDepartmentcode(pro.get); 部门编号缺失
		app.setProjectcode(pro.getId().toString());
		app.setStatus("0");
		app.setCustcode(pro.getCustomerCode());
		app.setCustname(pro.getCustomerName());
		app.setCredentialnumber(pro.getPapersNo());
		app.setAccount("");
		app.setBank(pro.getBankDeposit());
		app.setBankno(pro.getBankAccount());
		app.setTransfercash(pro.getTransAmount()==null?new Double(0):pro.getTransAmount().doubleValue());
		//app.setTransferdate(pro.getTransDate());
		app.setIscode(pro.getFinancialPlannerCode());
		//app.setReservationSuccessDate(pro.getLineupSuccess());
		app.setReserveStatus(pro.getBesStatus());
		//app.setReserveDate(pro.getBesDate());
		return app;
	}
	
	public void reserveDuojinData(Date date){
		List<String> list = new ArrayList<String>();
		list.add("1");
		list.add("d");
		list.add("7");
		list.add("2");
		Object obj = null;
		ContractDetail detail = new ContractDetail();
		List<Appoint> apps = new ArrayList<Appoint>();
		if(date != null){
			Map map = tpContractService.getAddReserveMsgByDate(list,new Date());
			obj = map.get("list");
			List<TgetAllReserveMsgByStatusVO> res = obj != null?(List<TgetAllReserveMsgByStatusVO>)obj:new ArrayList();
			if(res.size() > 0){
				for(TgetAllReserveMsgByStatusVO reser : res){
					Appoint app = new Appoint();
					app.setReserveNo(reser.getReserveNo());
					if(appointService.findCount(app) > 0){
						appointService.update(buildAppoint(reser));
						continue;
					}
					apps.add(buildAppoint(reser));	
				}
				detail.setApps(apps);
				appointService.insertBatch(detail);
			}
		}else{
			Map map = tpContractService.getReserveMsgCountByStatus(list);
			Integer total = Integer.valueOf(map.get("total").toString());
			Integer l = total/2000 + (total%2000==0?1:2);
			for(int i = 1 ; i < l ; i++){
				Map m = tpContractService.getAllReserveMsgByStatus(list, i, 2000);
				obj = m.get("list");
				List<TgetAllReserveMsgByStatusVO> res = obj != null?(List<TgetAllReserveMsgByStatusVO>)obj:new ArrayList();
				if(res.size() > 0){
					apps = new ArrayList<Appoint>();
					for(TgetAllReserveMsgByStatusVO reser : res){
						apps.add(buildAppoint(reser));	
					}
					detail.setApps(apps);
					appointService.insertBatch(detail);
				}
			}
		}
	}
	
	public void reserveComData(Date date){
		List<String> list = new ArrayList<String>();
		ContractDetail detail = new ContractDetail();
		List<Appoint> apps = new ArrayList<Appoint>();
		list.add("1");
		list.add("7");
		list.add("d");
		list.add("e");
		String status = "1";
		int cl = 1; 
		for(int i = 0 ; i < cl ; i++){
			InvokeResultWithGenericType<Page<ProjectBespeakDetailPO>> invokeResultWith = bespeakInfoService.getAllBespeak(status,null,date, i,2000);
			Page<ProjectBespeakDetailPO> page = invokeResultWith.getData();
			apps = new ArrayList<Appoint>();
			for(ProjectBespeakDetailPO pro : invokeResultWith.getData().getRecords()){
				apps.add(buildAppoint(pro));
			}
			detail.setApps(apps);
			appointService.insertBatch(detail);
		}
	}
	
	
	@Scheduled(fixedRate = 1000*60*5)   
    public void appointScheduler(){
		List<String> list = new ArrayList<String>();
		Object obj = null;
		Date date = appointService.findCount(new Appoint())>0?new Date():null;
		System.out.println("------------多金数据同步开始---------" + new Date() + "--------");
		long na = System.nanoTime();
		reserveDuojinData(date);
		System.out.println("------------多金数据同步结束---------" + new Date() + "--------");
		long cl = System.nanoTime();
		System.out.println("------------多金数据同步耗时---------" + (na - cl)/(10000*1000*60) + "分钟--------");
		System.out.println("------------通用数据同步开始---------" + new Date() + "--------");
		//reserveComData(date);
		System.out.println("------------通用数据同步结束---------" + new Date() + "--------");
		System.out.println("------------通用数据同步耗时---------" + (System.nanoTime() - cl)/10000 + "毫秒--------");
    } 
}
